﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MapObjects;

namespace MOClient
{
    public partial class MainForm : Form
    {
        private DataConnection conn;
        private MapLayer layer1;
        private MapLayer layer2;
        private MapLayer layer3;
        private MapLayer layer4;
        private MapLayer layer5;
        private MapLayer layer6;
        SymbolClass s;

        SelectByAttributesForm f = null;

        public MainForm()
        {
            InitializeComponent();
            this.Text = "MOclient" + DateTime.Now.ToShortTimeString();
        }

        public AxMapObjects.AxMap Map
        {
            get { return axMap1; }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadLayers();
            LoadLayersNavigation();

            SetupWebLink();
            MapObjects.Rectangle ext = axMap1.FullExtent;
            ext.ScaleRectangle(2);
            //ext.ScaleRectangle(0.5);
            MapObjects.Rectangle ext2 = ext;
        }

        private void SetupWebLink()
        {
            axWebLink1.Start();
            axWebLink1.MapPort = 5062;
            axWebLink1.LocalIp = "";
        }

        private void LoadLayersNavigation()
        {
            for (int i = 0; i < axMap1.Layers.Count; i++)
            {
                MapLayerClass layer = (MapLayerClass)axMap1.Layers.Item(i);
                checkedListBox1.Items.Add(layer.name, true);
                layer.Visible = true;
            }
        }

        private void LoadLayers()
        {
            conn = new DataConnection();
            conn.Database = @"E:\Veng\MAPS\";

            axMap1.Layers.Clear();

            layer1 = new MapLayerClass();
            layer1.GeoDataset = conn.FindGeoDataset("regions");
            axMap1.Layers.Add(layer1);

            layer2 = new MapLayerClass();
            layer2.GeoDataset = conn.FindGeoDataset("ukr_obl");
            axMap1.Layers.Add(layer2);

            layer3 = new MapLayerClass();
            layer3.GeoDataset = conn.FindGeoDataset("centrobl");
            axMap1.Layers.Add(layer3);

            layer4 = new MapLayerClass();
            layer4.GeoDataset = conn.FindGeoDataset("zalizn");
            axMap1.Layers.Add(layer4);


            layer6 = new MapLayerClass();
            layer6.GeoDataset = conn.FindGeoDataset("terrytor");
            axMap1.Layers.Add(layer6);

            layer5 = new MapLayerClass();
            layer5.GeoDataset = conn.FindGeoDataset("nas_punk");
            axMap1.Layers.Add(layer5);

            //axMap1.Extent = axMap1.FullExtent;
            axMap1.Refresh();
        }

        private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
        {

            MapLayerClass layer = (MapLayerClass)axMap1.Layers.Item(checkedListBox1.SelectedIndex);

            layer.Visible = checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex);

            axMap1.Refresh();
        }

        private void selectByAttributesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            f = new SelectByAttributesForm(axMap1.Layers, this);
            f.Show(this);
        }

        private void selectByLocationToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void axMap1_AfterTrackingLayerDraw(object sender, AxMapObjects._DMapEvents_AfterTrackingLayerDrawEvent e)
        {
            Recordset r = f.glrecs;
            if (!r.EOF)
            {
                //  MessageBox.Show(r.Count.ToString());
                s = new SymbolClass();
                s.SymbolType = (short)SymbolTypeConstants.moFillSymbol;
                s.Size = 1;
                s.Color = Convert.ToUInt32(ColorConstants.moCyan);
                s.Style = (short)FillStyleConstants.moGrayFill;
                while (!r.EOF)
                {
                    axMap1.DrawShape(r.Fields.Item("Shape").Value, s);
                    r.MoveNext();
                }
            }
        }

        private void axMap1_AfterLayerDraw(object sender, AxMapObjects._DMapEvents_AfterLayerDrawEvent e)
        {

        }
    }
}
